/**
 * File ControlAgent.java created by Pablo Díaz Fernández
 * on 28/10/2011 for the project supply-chain-simulator
 */
package org.lsk.pfc.scsim.model.agent;

import java.util.ArrayList;
import java.util.List;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;

import org.lsk.pfc.scsim.model.behaviour.MASSyndicationBehaviour;
import org.lsk.pfc.scsim.model.util.ConstantKeys;

/**
 * @author Pablo Díaz Fernández
 * 
 */
public final class ControlAgent extends Agent {

	/**
	 * Serial version ID.
	 */
	private static final long serialVersionUID = -6224732055666665077L;

	/**
	 * Constant for a customer MAS role.
	 */
	public static final int CUSTOMER = 0;
	/**
	 * Constant for a basic MAS role.
	 */
	public static final int BASIC = 1;
	/**
	 * Constant for a manufacturer MAS role.
	 */
	public static final int MANUFACTURER = 2;

	/**
	 * MAS role.
	 */
	private Integer role;

	/**
	 * MAS name.
	 */
	private String masName;

	/**
	 * MAS tree level.
	 */
	private Integer level;

	/**
	 * {@link AID} of the information agent of this MAS.
	 */
	private AID informationAgentAID;

	/**
	 * @see jade.core.Agent#setup()
	 */
	@Override
	protected void setup() {
		masName = (String) super.getArguments()[0];
		role = (Integer) super.getArguments()[1];
		level = (Integer) super.getArguments()[2];

		informationAgentAID = new AID(masName + ConstantKeys.IA_SUFFIX,
		        AID.ISLOCALNAME);
		registerYellowPages();
		addBehaviour(new MASSyndicationBehaviour());
	}

	/**
	 * 
	 * @see jade.core.Agent#takeDown()
	 */
	@Override
	protected void takeDown() {
		try {
			DFService.deregister(this);
		} catch (FIPAException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Registers this MAS in the yellow pages.
	 */
	private void registerYellowPages() {
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType(level.toString());
		sd.setName(masName);
		dfd.addServices(sd);
		try {
			DFService.register(this, dfd);
		} catch (FIPAException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Gets the list of current providers available.
	 * 
	 * @return {@link List} of {@link AID} of the {@link ControlAgent} of the
	 *         provider MAS.
	 */
	public List<AID> getProviders() {
		List<AID> providers = new ArrayList<AID>();
		DFAgentDescription template = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType(String.valueOf(level + 1));
		template.addServices(sd);
		try {
			DFAgentDescription[] result = DFService.search(this, template);
			for (int i = 0; i < result.length; ++i) {
				providers.add(result[i].getName());
			}
		} catch (FIPAException fe) {
			fe.printStackTrace();
		}
		return providers;
	}

	/**
	 * Gets the role.
	 * 
	 * @return the role
	 */
	public int getRole() {
		return role;
	}

	/**
	 * Gets the masName.
	 * 
	 * @return the masName
	 */
	public String getMasName() {
		return masName;
	}

	/**
	 * Gets the informationAgentAID.
	 * 
	 * @return the informationAgentAID
	 */
	public AID getInformationAgentAID() {
		return informationAgentAID;
	}

	/**
	 * Gets the level.
	 * 
	 * @return the level
	 */
	public Integer getLevel() {
		return level;
	}
}
